package com.thread.www;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * Created by 27340 on 2017/3/26.
 */
public class CylicBarrierTest {
    public static void main(String[] args){
        CyclicBarrier barrier = new CyclicBarrier(3 , new TotalTask());
        BillTask woker1 = new BillTask("111" , barrier);
        BillTask woker2 = new BillTask("112" , barrier);
        BillTask woker3 = new BillTask("113" , barrier);
        woker1.start();woker2.start();woker3.start();
        System.out.println("main thred is end");
    }

    static class TotalTask extends Thread{
        @Override
        public void run() {
            System.out.println("所有的子任务都执行完成，开始执行主任务");
        }
    }
    static class BillTask extends Thread{
        private String billName;
        private CyclicBarrier barrier;
        public BillTask(String billName , CyclicBarrier barrier){
            this.billName = billName;
            this.barrier = barrier;
        }
        public void run(){
            try {
                System.out.println("市区 ： " + billName + "运算开始");
                Thread.sleep(1000);
                System.out.println("市区 ： " + billName + "运算完成，等待中");
                barrier.await();
                System.out.println("全部都结束，市区：" + billName + "开始后面的工作");

            }catch (InterruptedException e){
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }
}
